// pages/posts/post-detail/post-detail.js
var postsData = require('../../../data/posts-data.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 标记是否正在播放音乐
    isPlayingMusic : false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options id is " + options.id);
    var postId = options.id;
    this.data.currentPostId = postId;
    var postData = postsData.postList[postId];
    this.setData({
      postData : postData
    });

    // 读取缓存内容, 更新UI
    var postsCollected = wx.getStorageSync('posts_collection');
    if(postsCollected) {
      var postCollectedStatus = postsCollected[postId];
      this.setData({
        collected: postCollectedStatus
      });
    } else {
      var postsCollected = {}
      postsCollected[postId] = false;
      wx.setStorageSync('posts_collection', postsCollected);
    }

    // 同步播放按钮状态
    var isPlaying = app.globalData.g_isPlayingMusic;
    var playingPostId = app.globalData.g_currentMusicPostId;

    if (isPlaying && playingPostId == postId) {
      this.setData({
        isPlayingMusic : true
      })
    }

    // 监听 Music 播放状态
    this.setMusicMonitor();
  },

  // 设置监听 Music 播放状态
  setMusicMonitor:function() {

    // this 所指的是程序运行的上下文环境
    var that = this;
    // 监听音乐开始播放
    wx.onBackgroundAudioPlay(function () {
      console.log("开始播放");
      // 如果此时扔用this, 会报错"this.setData is not a function""
      that.setData({
        isPlayingMusic: true
      });
    });

    // 监听音乐暂停播放
    wx.onBackgroundAudioPause(function () {
      console.log("暂停播放");
      that.setData({
        isPlayingMusic: false
      });
    });

    // 监听音乐停止播放
    wx.onBackgroundAudioStop(function () {
      console.log("停止播放");
    })
  }, 

  // 缓存结构
  // var postsCollected = {
  //       1 : true,
  //       2 : false,
  //       3 : false
  // }

// 点击收藏
  onColletionTap:function(options) {
    var postsCollected = wx.getStorageSync('posts_collection');
    // 取出收藏状态
    var postCollectedStatus = postsCollected[this.data.currentPostId];
    // 取反
    postCollectedStatus = !postCollectedStatus;
    // 改变页面
    this.setData({
      collected: postCollectedStatus
    });

    // 显示吐司
    wx.showToast({
      title: postCollectedStatus ? '收藏成功' : '取消成功',
      duration:800,
      icon:'success',
      success:function() {

      },
      fail:function() {

      }, 
      complete:function() {

      }
    })
    // wx.showModal({
    //   title: '你好',
    //   content: '你好吗',
    // })

    // 改变缓存
    postsCollected[this.data.currentPostId] = postCollectedStatus;
    wx.setStorageSync("posts_collection", postsCollected);
  },

// 点击分享
onShareTap:function(options) {
  var itemList = [
    "分享到朋友圈",
    "分享给好友",
    "分享到QQ"
  ];
  wx.showActionSheet({
    itemList: itemList,
    success : function(res) {
      wx.showToast({
        title: '' + itemList[res.tapIndex],
      })
    }
  })
},

// 点击播放按钮
onMusicTap:function(options) {
  var postData = postsData.postList[this.data.currentPostId];
  var isPlayingMusic = this.data.isPlayingMusic;

  if (isPlayingMusic) {
    // 暂停播放
    wx.pauseBackgroundAudio();
    this.setData({
      isPlayingMusic : false
    })
    app.globalData.g_isPlayingMusic = false;
  } else {
    // 播放
    wx.playBackgroundAudio({
      dataUrl: postData.music.url,
      title: postData.music.title,
      coverImgUrl: postData.music.coverImg,
      success:function(option) {
        console.log("播放成功");
      },
      fail: function (option) {
        console.log("播放失败");
      }
    })
    this.setData({
      isPlayingMusic: true
    })
    app.globalData.g_currentMusicPostId = this.data.currentPostId;
    app.globalData.g_isPlayingMusic = true;
  }

},

onShareAppMessage:function(event) {
  return {
    title:"测试",
    desc:"曾经沧海难为水，除却巫山不是云",
    path:"/pages/posts/post-detail/post-detail?id=1"    
  }
}

})